package org.ronghao.androidwifiplatfrom.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MessageUtils {
	// TODO:修改包名
	public MessageUtils() {
	}

	/**
	 * 首字母大写
	 * 
	 * @author ronghao
	 * @data 2012-11-26 下午1:36:18
	 * @param str
	 * @return
	 */
	public static String firstLetterToUpper(String str) {
		Character c = Character.toUpperCase(str.charAt(0));
		return c.toString().concat(str.substring(1));
	}

	/**
	 * 要发送的消息
	 * 
	 * @author ronghao
	 * @data 2012-11-23 上午11:07:36
	 * @param object
	 * @param result
	 */
	public static String setData(Object object) {
		JSONObject result = null;
		result = new JSONObject();
		Field[] fields = object.getClass().getDeclaredFields();
		for (Field field : fields) {
			try {
				result.put(
						field.getName(),
						object.getClass()
								.getMethod(
										"get"
												+ firstLetterToUpper(field
														.getName()))
								.invoke(object));
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return result.toString();
	}

	/**
	 * 反射机制处理返回的东西
	 * 
	 * @author ronghao
	 * @data 2012-11-22 下午5:13:06
	 * @param object
	 * @param result
	 */
	public static Object getData(Class<?> myClass, String str) {
		JSONObject result = null;
		Object object = null;
		try {
			result = new JSONObject(str);
			object = myClass.newInstance();
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return null;
		} catch (InstantiationException e) {
			e.printStackTrace();
			return null;
		}

		Field[] fields = object.getClass().getDeclaredFields();
		for (Field field : fields) {
			try {
				String type = field.getType().getName();
				object.getClass()
						.getMethod("set" + firstLetterToUpper(field.getName()),
								field.getType())
						.invoke(object,
								result.getClass()
										.getMethod(
												"get"
														+ firstLetterToUpper(type
																.substring(type
																		.lastIndexOf(".") + 1)),
												String.class)
										.invoke(result, field.getName()));

			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		return object;
	}
	
	
	@SuppressWarnings("unchecked")
	public static <E> List<E> getJSONArray(Class<E> theClass, String str) {
		List<E> list = new ArrayList<E>();
		JSONArray jsonArray = null;
		Object item = null;
		try {
			jsonArray = new JSONArray(str);
			for (int i = 0; i < jsonArray.length(); i++) {
				item = (E) theClass.newInstance();
//				JSONObject result = null;
//				result = new JSONObject((jsonArray.get(i)).toString());
				item =MessageUtils.getData(item.getClass(), (jsonArray.get(i)).toString());
				list.add((E) item);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
		return list;
	}
}
